<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数字转货币格式示例</title>
    <style>
        body {
            font-family: 'Arial', sans-serif;
            background-color: #f4f7fc;
            margin: 0;
            padding: 0;
            display: flex;
            justify-content: center;
            align-items: center;
            height: 100vh;
        }

        .container {
            background-color: #fff;
            padding: 30px;
            border-radius: 8px;
            box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
            width: 100%;
            max-width: 600px;
            text-align: center;
        }

        h1 {
            font-size: 24px;
            color: #333;
            margin-bottom: 20px;
        }

        label {
            font-size: 18px;
            color: #555;
            display: block;
            margin-bottom: 10px;
        }

        input[type="number"] {
            padding: 10px;
            font-size: 18px;
            border: 1px solid #ddd;
            border-radius: 4px;
            width: 100%;
            margin-bottom: 20px;
            box-sizing: border-box;
        }

        button {
            background-color: #007bff;
            color: white;
            padding: 10px 20px;
            font-size: 16px;
            border: none;
            border-radius: 4px;
            cursor: pointer;
            transition: background-color 0.3s;
        }

        button:hover {
            background-color: #0056b3;
        }

        .result {
            margin-top: 20px;
            font-size: 20px;
            font-weight: bold;
            color: #333;
        }
    </style>
</head>

<body>
    <div class="container">
        <h1>数字转货币格式</h1>
        <label for="inputNumber">请输入金额：</label>
        <input type="number" id="inputNumber" placeholder="输入一个数字" />

        <button id="convertButton">转换为货币格式</button>

        <div class="result" id="result">
            <!-- 转换后的货币格式会显示在这里 -->
        </div>
    </div>

    <script>
        // 数字转货币格式的函数
        const numberToCurrency = (num, format = 'zh-CN', options = { style: 'currency', currency: 'CNY' }) => {
            return num.toLocaleString(format, options);
        };

        // 获取页面元素
        const inputNumber = document.getElementById('inputNumber');
        const convertButton = document.getElementById('convertButton');
        const resultDiv = document.getElementById('result');

        // 按钮点击事件处理
        convertButton.addEventListener('click', () => {
            const num = parseFloat(inputNumber.value);

            if (isNaN(num)) {
                resultDiv.textContent = '请输入有效的数字';
                resultDiv.style.color = 'red';
            } else {
                const formattedCurrency = numberToCurrency(num);
                resultDiv.textContent = `转换后的金额是：${formattedCurrency}`;
                resultDiv.style.color = '#333';
            }
        });
    </script>
</body>

</html>